#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import yaml
import sys
import logging

# 配置日志记录
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# 定义测试用例
test_cases = {
    "addition": {
        "valid": [
            {
                "input": {"a": 10, "b": 20},
                "expected": 30,
                "actual": None,
                "description": "正整数 + 正整数"
            },
            {
                "input": {"a": -10, "b": -20},
                "expected": -30,
                "actual": None,
                "description": "负整数 + 负整数"
            },
            {
                "input": {"a": 10, "b": -20},
                "expected": -10,
                "actual": None,
                "description": "正整数 + 负整数（绝对值正整数大于负整数）"
            },
            {
                "input": {"a": -10, "b": 20},
                "expected": 10,
                "actual": None,
                "description": "正整数 + 负整数（绝对值正整数小于负整数）"
            },
            {
                "input": {"a": 10, "b": 0},
                "expected": 10,
                "actual": None,
                "description": "整数 + 0"
            },
            {
                "input": {"a": 10.5, "b": 20.5},
                "expected": 31.0,
                "actual": None,
                "description": "小数 + 小数"
            },
            {
                "input": {"a": 10.5, "b": 20},
                "expected": 30.5,
                "actual": None,
                "description": "小数 + 整数"
            },
            {
                "input": {"a": -2**31, "b": 2**31 - 1},
                "expected": -1,
                "actual": None,
                "description": "最小整数 + 最大整数"
            },
            {
                "input": {"a": sys.float_info.min, "b": sys.float_info.min},
                "expected": 2 * sys.float_info.min,
                "actual": None,
                "description": "最小可表示小数 + 最小可表示小数"
            },
            {
                "input": {"a": sys.float_info.max, "b": -sys.float_info.max},
                "expected": 0,
                "actual": None,
                "description": "最大可表示小数 + 最大可表示小数的负数"
            },
            {
                "input": {"a": -1, "b": 0},
                "expected": -1,
                "actual": None,
                "description": "-1 + 0"
            },
            {
                "input": {"a": 0, "b": 1},
                "expected": 1,
                "actual": None,
                "description": "0 + 1"
            }
        ],
        "invalid": [
            {
                "input": {"a": "10", "b": 20},
                "expected": "TypeError",
                "actual": None,
                "description": "非数字类型（如字符串）+ 数字"
            },
            {
                "input": {"a": 10, "b": "20"},
                "expected": "TypeError",
                "actual": None,
                "description": "数字 + 非数字类型（如字符串）"
            }
        ]
    }
}

# 将测试用例写入YAML文件
def save_test_cases_to_yaml(test_cases, file_path="test_cases.yaml"):
    try:
        if test_cases is None:
            raise ValueError("test_cases cannot be None")

        with open(file_path, "w", encoding="utf-8") as yaml_file:
            yaml.dump(test_cases, yaml_file, default_flow_style=False, allow_unicode=True)
        logging.info(f"Test cases successfully saved to {file_path}")
    except FileNotFoundError:
        logging.error(f"The directory of the file path {file_path} does not exist.")
    except PermissionError:
        logging.error(f"Permission denied when trying to write to {file_path}.")
    except Exception as e:
        logging.error(f"An error occurred while saving test cases: {e}")

# 读取YAML文件
def load_test_cases_from_yaml(file_path="test_cases.yaml"):
    try:
        with open(file_path, "r", encoding="utf-8") as yaml_file:
            loaded_test_cases = yaml.safe_load(yaml_file)
        logging.info(f"Test cases successfully loaded from {file_path}")
        return loaded_test_cases
    except FileNotFoundError:
        logging.error(f"The file {file_path} does not exist.")
    except PermissionError:
        logging.error(f"Permission denied when trying to read {file_path}.")
    except Exception as e:
        logging.error(f"An error occurred while loading test cases: {e}")
    return None

# 示例调用
save_test_cases_to_yaml(test_cases)
loaded_test_cases = load_test_cases_from_yaml()
if loaded_test_cases:
    print("加载的测试用例:", loaded_test_cases)
else:
    print("加载测试用例失败")
